package com.lt.common;

import android.app.ActivityManager;
import android.content.Context;
import android.text.TextUtils;

import com.lt.base.BaseApplication;

import java.util.ArrayList;

public class ServiceHelper {

    private static ServiceHelper instance;

    private ServiceHelper() {
    }

    public static ServiceHelper getInstance() {
        if (instance == null) {
            synchronized (ServiceHelper.class) {
                instance = new ServiceHelper();
            }
        }
        return instance;
    }

    /**
     * @return 判断服务是否开启
     */
    public boolean isServiceRunning(String ServiceName) {
        if (TextUtils.isEmpty(ServiceName)) {
            return false;
        }
        ActivityManager myManager = (ActivityManager) BaseApplication.getContext().getSystemService(Context.ACTIVITY_SERVICE);
        if (myManager != null) {
            ArrayList<ActivityManager.RunningServiceInfo> runningService = (ArrayList<ActivityManager.RunningServiceInfo>) myManager.getRunningServices(1000);
            for (int i = 0; i < runningService.size(); i++) {
                if (runningService.get(i).service.getClassName().equals(ServiceName)) {
                    return true;
                }
            }
        }
        return false;
    }

}
